function disableSubmit(finalResult,submitButtonId) {
	if(finalResult) {
		document.getElementById(submitButtonId).disabled = true;
		return finalResult;
	}else {
		return finalResult;
	}
}

function batchDelete(action,checkboxName,form){
    if (!hasOneChecked(checkboxName)){
            alert('请选择要操作的对象!');
            return;
    }
    if (confirm('确定执行[删除]操作?')){
        form.action = action;
        form.submit();
    }
}

function hasOneChecked(name){
    var items = document.getElementsByName(name);
    if (items.length > 0) {
        for (var i = 0; i < items.length; i++){
            if (items[i].checked == true){
                return true;
            }
        }
    } else {
        if (items.checked == true) {
            return true;
        }
    }
    return false;
}

function setAllCheckboxState(name,state) {
	var elms = document.getElementsByName(name);
	for(var i = 0; i < elms.length; i++) {
		elms[i].checked = state;
	}
}

function getReferenceForm(elm) {
	while(elm && elm.tagName != 'BODY') {
		if(elm.tagName == 'FORM') return elm;
		elm = elm.parentNode;
	}
	return null;
}

function changeSortAndPage(pageNumber,sortName)
{
	var sortColumns = document.forms[0].sortColumns.value;
	var sorts = sortColumns.split(" ");
	
	var column = sorts[0];
	var order = sorts.length > 1 ? sorts[1]:'asc';
	
	if(column == sortName){
		if(order == 'asc'){
			document.forms[0].sortColumns.value = sortName + ' ' + 'desc';
		}else{
			document.forms[0].sortColumns.value = sortName + ' ' + 'asc';
		}
	}else{
		document.forms[0].sortColumns.value = sortName + ' ' + 'asc';
	}
	document.forms[0].pageNumber.value = pageNumber
	document.forms[0].submit();
}
function changePageNumber(pageNumber)
{
	document.forms[0].pageNumber.value = pageNumber
	document.forms[0].submit();
}
function toggleSelection(obj, collection, firstElement){
	if (obj.className == 'btn btn-default'){	        		
		//if 'All' selected
		if ($(obj).attr('data-code') =='0'){	        			
			$(collection).removeClass('btn-default').addClass('btn-success');
		}
		else{
			obj.className = 'btn btn-success';	  
		}
	}
	else{	    
		//if 'All' unselected
		if ($(obj).attr('data-code') =='0'){
			$(collection).removeClass('btn-success').addClass('btn-default');
		}
		else{
			obj.className = 'btn btn-default';
			
		}
		$(firstElement).removeClass('btn-success').addClass('btn-default');
	}  
};

function addDatePicker(selector){
	$(selector).datetimepicker({
                weekStart: 1,
                todayBtn: 1,
                autoclose: 1,
                todayHighlight: 1,
                startView: 2,
                minView: 2,
                forceParse: 0
            });
};

function addDateTimePicker(selector){
	$(selector).datetimepicker({
                weekStart: 1,
                todayBtn: 1,
                autoclose: 1,
                todayHighlight: 1,
                startView: 2,
                forceParse: 0
            });
};

function toggleIcon(selector){
	$(selector).click(function(){  
		var span = $(this).children('span:first');
		if (span.hasClass('glyphicon-plus')) {
			span.removeClass('glyphicon-plus').addClass('glyphicon-minus');
		}      	
		else{
			span.removeClass('glyphicon-minus').addClass('glyphicon-plus');
		}
    });
};

function addPaginator(selector, currentPage, totalPages){
	var options = {
		bootstrapMajorVersion: 3,
		alignment: "right",
	    currentPage: currentPage,
	    numberOfPages: 10,
	    totalPages: totalPages,	    
	    onPageClicked: function(e,originalEvent,type,page){
	    	e.stopImmediatePropagation();            	
	    	var currentTarget = $(e.currentTarget);
	        var currentPage = currentTarget.bootstrapPaginator("getPages").current;
	        
	        if (currentPage != page){
	    		changePageNumber(page);
	    	}	    	
	    	return;
	    }
    }
    
    $(selector).bootstrapPaginator(options);
}

function showValidationError(field, message){
	$('[name='+ field + ']').after('<div class="alert alert-danger" style="padding:6px;margin-bottom:0px">' + message['' + field + ''] +'</div>');                	
}
